﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
   public class HoaDonNhapDAL
    {
        public static HoaDonNhapObj GetByID(string HoadonnhapID)
        {
            HoaDonNhapObj obj = new HoaDonNhapObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbHoaDonNhap_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@HoadonnhapID", HoadonnhapID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }

        public static List<HoaDonNhapObj> GetByAll()
        {
            List<HoaDonNhapObj> result = new List<HoaDonNhapObj>();
            HoaDonNhapObj obj = new HoaDonNhapObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbHoaDonNhap_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static bool Insert(HoaDonNhapObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbHoaDonNhap_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Ngaylap", (obj.Ngaylap == null) ? String.Empty : obj.Ngaylap);
                    cmd.Parameters.AddWithValue("@Nhanvien", (obj.Nhanvien == null) ? String.Empty : obj.Nhanvien);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Update(HoaDonNhapObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbHoaDonNhap_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@HoadonnhapID", (obj.HoadonnhapID == null) ? String.Empty : obj.HoadonnhapID);
                    cmd.Parameters.AddWithValue("@Ngaylap", (obj.Ngaylap == null) ? String.Empty : obj.Ngaylap);
                    cmd.Parameters.AddWithValue("@Nhanvien", (obj.Nhanvien == null) ? String.Empty : obj.Nhanvien);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Delete(string HoadonnhapID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbHoaDonNhap_DeleteByID";
                    cmd.Parameters.AddWithValue("@HoadonnhapID", HoadonnhapID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
